package cn.wolfcode.controller;


import cn.wolfcode.entity.Department;
import cn.wolfcode.service.IDepartmentService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yunan
 * @since 2022-12-13
 */
@RestController
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    @PostMapping("/departments")
    public Department save(@RequestBody Department department){
        boolean save = departmentService.save(department);
        return department;

    }

    @PutMapping("/departments/{id}")
    public Department put(@PathVariable("id") Long id,@RequestBody Department department){
        department.setId(id);
        departmentService.updateById(department);
        return department;
    }

    @DeleteMapping("/departments/{id}")
    public Department delete(@PathVariable("id") Long id){
        departmentService.removeById(id);
        return new Department();
    }

    @GetMapping("/departments")
    public List<Department> list(){
        List<Department> list = departmentService.list();
        return list;
    }
    @GetMapping("/departments/{id}")
    public Department get(@PathVariable("id") Long id){
        Department department = departmentService.getById(id);
        return department;

    }
}

